/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.token.dao.AbstractDAO;
import com.opgea.token.dao.LoginDAO;
import com.opgea.token.domain.entities.Login;

/**
 * 
 * @author Ramesh
 */
@Repository
@Transactional
public class LoginDAOImpl extends AbstractDAO<Login> implements LoginDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public Login find(Long id) {
	Session session = sessionFactory.getCurrentSession();
	Login login = (Login) session.get(Login.class, id);
	return login;
    }

    public Login findByLoginId(String loginId) {
	Session session = sessionFactory.getCurrentSession();
	Login login = (Login) session.createQuery(
		"Select l from Login l where l.loginId = '" + loginId + "'")
		.uniqueResult();
	return login;
    }

    @SuppressWarnings("unchecked")
    public List<Login> findAll() {
	Session session = sessionFactory.getCurrentSession();
	List<Login> logins = session.createQuery("from Login").list();
	System.out.println("No. of Users: " + logins.size());
	return logins;
    }

}
